{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "a1a0cf63",
   "metadata": {},
   "source": [
    "# Evaluate --- Speaker Verification (SV)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "abdc0067",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Auto-reload imported modules from sslsv\n",
    "%load_ext autoreload\n",
    "%autoreload 2\n",
    "\n",
    "# Load sslsv as a package from the parent folder\n",
    "import os\n",
    "import sys\n",
    "os.chdir('../..')\n",
    "sys.path.insert(1, os.path.join(sys.path[0], '../..'))\n",
    "\n",
    "# Embed fonts when saving figures as PDF\n",
    "import matplotlib\n",
    "matplotlib.rc('pdf', fonttype=42)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7fccb6bd",
   "metadata": {},
   "outputs": [],
   "source": [
    "from notebooks.notebooks_utils import (\n",
    "    load_models,\n",
    "    evaluate_models,\n",
    "    create_metrics_df\n",
    ")\n",
    "\n",
    "from sv_visualization import (\n",
    "    det_curve,\n",
    "    scores_distribution,\n",
    "    tsne_3D,\n",
    "    tsne_2D,\n",
    "    pca_2D\n",
    ")\n",
    "\n",
    "from sslsv.evaluations.CosineSVEvaluation import CosineSVEvaluation, CosineSVEvaluationTaskConfig"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e7c468fc",
   "metadata": {},
   "outputs": [],
   "source": [
    "models = load_models(\n",
    "    [\n",
    "        './models/old/vox2_ddp_sntxent_s=30_m=0/config.yml',\n",
    "        './models/old/vox2_ddp_sntxent_s=30_m=0.1/config.yml'\n",
    "    ],\n",
    "    override_names={\n",
    "        'models/old/vox2_ddp_sntxent_s=30_m=0'   : 'simclr',\n",
    "        'models/old/vox2_ddp_sntxent_s=30_m=0.1' : 'simclr_am'\n",
    "    }\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "144dd8c6",
   "metadata": {},
   "outputs": [],
   "source": [
    "evaluate_models(models, CosineSVEvaluation, CosineSVEvaluationTaskConfig())"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1af58437",
   "metadata": {},
   "source": [
    "## Metrics"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b592d171",
   "metadata": {},
   "outputs": [],
   "source": [
    "create_metrics_df(models)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "be0dbe8c",
   "metadata": {},
   "source": [
    "## Detection Error Tradeoff (DET)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2807476d",
   "metadata": {},
   "outputs": [],
   "source": [
    "det_curve(models)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "43144146",
   "metadata": {},
   "source": [
    "## Scores distribution"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ee05af45",
   "metadata": {},
   "outputs": [],
   "source": [
    "scores_distribution(models, use_angle=False)#.save('score_distribution.pdf')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e4874532",
   "metadata": {},
   "source": [
    "## t-SNE of speaker embeddings"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2f8f77d0",
   "metadata": {},
   "outputs": [],
   "source": [
    "tsne_3D(models['simclr'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "387760bf",
   "metadata": {},
   "outputs": [],
   "source": [
    "tsne_2D(models['simclr'])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "59c8b297",
   "metadata": {},
   "source": [
    "## PCA of speaker embeddings"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "76d0aba9",
   "metadata": {},
   "outputs": [],
   "source": [
    "PCA_SPEAKERS = ['id10276', 'id10278', 'id10292', 'id10293', 'id10307', 'id10309']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "23860ee9",
   "metadata": {},
   "outputs": [],
   "source": [
    "pca_2D(models['simclr'], speakers=PCA_SPEAKERS)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "afeb60e1",
   "metadata": {},
   "source": [
    "### Visualize different principal components"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f71366db",
   "metadata": {},
   "outputs": [],
   "source": [
    "pca_2D(models['simclr'], components=[1, 2], speakers=PCA_SPEAKERS)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e81c1686",
   "metadata": {},
   "outputs": [],
   "source": [
    "pca_2D(models['simclr'], components=[1, 2], nb_speakers=10, nb_samples=150)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7d785499",
   "metadata": {},
   "outputs": [],
   "source": [
    "pca_2D(models['simclr'], components=[2, 3], speakers=PCA_SPEAKERS)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
